package com.jjq;

import org.apache.hadoop.io.Writable;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/***
 *
 * @author Administrator
 * 1:write 是把每个对象序列化到输出流
 * 2:readFields是把输入流字节反序列化
 * 3:实现WritableComparable
 *      Java值对象的比较：一般需要重写toString(),hashCode(),equals()方法
 *
 */
public class FlowBean implements Writable {

    private String phoneNumber;//电话号码
    private long upFlow;//上行流量
    private long downFlow;//下行流量
    private long sumFlow;//总流量



    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
    public long getUpFlow() {
        return upFlow;
    }
    public void setUpFlow(long upFlow) {
        this.upFlow = upFlow;
    }
    public long getDownFlow() {
        return downFlow;
    }
    public void setDownFlow(long downFlow) {
        this.downFlow = downFlow;
    }
    public long getSumFlow() {
        return sumFlow;
    }
    public void setSumFlow(long sumFlow) {
        this.sumFlow = sumFlow;
    }

    //为了对象数据的初始化方便，加入一个带参的构造函数
    public FlowBean(String phoneNumber, long upFlow, long downFlow) {
        this.phoneNumber = phoneNumber;
        this.upFlow = upFlow;
        this.downFlow = downFlow;
        this.sumFlow = upFlow + downFlow;
    }
    //在反序列化时候，反射机制需要调用空参的构造函数，所以定义了一个空参的构造函数
    public FlowBean() {
    }

    //重写toString()方法
    @Override
    public String toString() {
        return "" + upFlow + "\t" + downFlow + "\t" + sumFlow + "";
    }


    //从数据流中反序列出对象的数据
    //从数据流中读取字段时必须和序列化的顺序保持一致
    public void readFields(DataInput in) throws IOException {
        phoneNumber = in.readUTF();
        upFlow = in.readLong();
        downFlow = in.readLong();
        sumFlow = in.readLong();

    }

    //将对象数据序列化到流中
    public void write(DataOutput out) throws IOException {
        out.writeUTF(phoneNumber);
        out.writeLong(upFlow);
        out.writeLong(downFlow);
        out.writeLong(sumFlow);

    }


}
